<?php
class MiscUtils {
  public static function dump($obj, $what = NULL) {
    if (defined ('DEBUG_ENABLED') && ! DEBUG_ENABLED) {
      return;
    }
    echo "<pre>\n";
    if (is_null ($obj)) {
      echo ($what !== NULL) ? "$what=!NULL!" : '!NULL!';
    } else if ($obj === '') {
      echo ($what !== NULL) ? "$what=!EMPTY!" : '!EMPTY!';
    } else if ($obj === false) {
      echo ($what !== NULL) ? "$what=!FALSE!" : '!FALSE!';
    } else {
      if ($what !== NULL) {
        echo "$what=<br>";
      }
      echo htmlspecialchars (print_r ($obj, true));
    }
    echo "</pre>\n";
  }

  public static function getParam($itm, $default = '', $trimText = false) {
    if (isset($_POST[$itm])) {
      return ($trimText) ? trim($_POST[$itm]) : $_POST[$itm];
    } else if (isset($_GET[$itm])) {
      return ($trimText) ? trim($_GET[$itm]) : $_GET[$itm];
    } else {
      return $default;
    }
  }

  public static function rrmdir($dir) {
    if (is_dir($dir)) {
      $objects = scandir($dir);
      foreach ($objects as $object) {
        if ($object != "." && $object != "..") {
          if (filetype($dir . "/". $object) == "dir") {
            MiscUtils::rrmdir($dir . "/". $object);
          } else {
            unlink($dir . "/" . $object);
          }
        }
      }
      reset($objects);
      rmdir($dir);
    }
  }

  public static function unique() {
    $chars = array();
    for ($i = 48, $il = 58; $i < $il; $i++) {
      $chars[] = chr($i);
    }
    for ($i = 65, $il = 91; $i < $il; $i++) {
      $chars[] = chr($i);
    }
    for ($i = 97, $il = 123; $i < $il; $i++) {
      $chars[] = chr($i);
    }

    $unique = '';
    for ($i = 0, $il = 16; $i < $il; $i++) {
      $unique .= $chars[rand(0, count($chars) - 1)];
    }
    return $unique;
  }

  public static function create($dir) {
    if (!is_dir($dir)) {
      $temp = explode('/',$dir);
      $cur_dir = '';
      for($i = 0; $i < count($temp); $i++) {
        $cur_dir .= $temp[$i].'/';
        if (!is_dir($cur_dir)) {
          @mkdir($cur_dir, 0777);
        }
      }
    }
  }

  public static function getShortName($str, $num) {
    if (strlen($str) <= $num) {
      return $str;
    }
    return mb_substr($str, 0, $num, 'utf-8') . '...';
  }

  function curPageURL() {
    $pageURL = 'http';
    if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
      $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
      $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
  }

  public static function convert($str,$ky=''){
    if($ky=='')return $str;
    $ky=str_replace(chr(32),'',$ky);
    if(strlen($ky)<8)exit('key error');
    $kl=strlen($ky)<32?strlen($ky):32;
    $k=array();for($i=0;$i<$kl;$i++){
      $k[$i]=ord($ky{$i})&0x1F;}
      $j=0;for($i=0;$i<strlen($str);$i++){
        $e=ord($str{$i});
        $str{$i}=$e&0xE0?chr($e^$k[$j]):chr($e);
        $j++;$j=$j==$kl?0:$j;}
        return $str;
  }
  
  public static function resize($im, $maxwidth, $maxheight, $name){
    $width = imagesx($im);
    $height = imagesy($im);
    if (($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)) {
      $RESIZEWIDTH = false;
      $RESIZEHEIGHT = false;
      if ($maxwidth && $width > $maxwidth) {
        $widthratio = $maxwidth/$width;
        $RESIZEWIDTH = true;
      }
      if ($maxheight && $height > $maxheight) {
        $heightratio = $maxheight/$height;
        $RESIZEHEIGHT = true;
      }
      if ($RESIZEWIDTH && $RESIZEHEIGHT) {
        if ($widthratio < $heightratio) {
          $ratio = $widthratio;
        } else {
          $ratio = $heightratio;
        }
      } else if ($RESIZEWIDTH) {
        $ratio = $widthratio;
      } else if ($RESIZEHEIGHT) {
        $ratio = $heightratio;
      }
      $newwidth = $width * $ratio;
      $newheight = $height * $ratio;
      if (function_exists("imagecopyresampled")) {
        $newim = imagecreatetruecolor($newwidth, $newheight);
        imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      } else {
        $newim = imagecreate($newwidth, $newheight);
        imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
      }
      imagejpeg($newim, $name . ".jpg");
      imagedestroy($newim);
    } else {
      imagejpeg($im, $name . ".jpg");
    }
  }
}
?>